package com.yanqu.road.server.timer.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionwar.UnionQunyingTimeInfo;
import com.yanqu.road.entity.activity.unionwar.enums.eTimeInfoPeriodType;
import com.yanqu.road.entity.activity.unionwar.result.CurrentTimeInfo;
import com.yanqu.road.logic.cross.CrossUnionWarRankListModel;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarActivityMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarRankMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.timer.GeneralAgentTimerMgr;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SyncUnionWarRankTask extends GeneralTask {

    private boolean isRun = false;

    private long lastCheckTime;

    @Override
    public void doRun() {
        if (!isRun) {
            isRun = true;
            long nowTime = System.currentTimeMillis();

            List<CrossUnionWarRankListModel> syncList = new ArrayList<>();
            Map<Integer, ActivityInfo> activityInfoMap = CrossUnionWarActivityMgr.getOpenActivityMap();
            if (nowTime - lastCheckTime > GeneralAgentTimerMgr.ALL_SYNC_TIME) {
                for (ActivityInfo activityInfo : activityInfoMap.values()) {
                    List<Integer> groupIdList = CrossUnionWarMgr.getGroupListByCrossServerId(activityInfo.getActivityId(), CrossBaseServer.getInstance().getServerId());
                    CurrentTimeInfo currentTimeInfo = CrossUnionWarActivityMgr.getCurrentTimeInfo(activityInfo.getActivityId());
                    UnionQunyingTimeInfo timeInfo = currentTimeInfo.getTimeInfo();
                    if (timeInfo == null && currentTimeInfo.getPeriodType() != eTimeInfoPeriodType.ReceiveTime) {
                        continue;
                    }
                    int phase = 2;
                    if(timeInfo != null){
                        phase = timeInfo.getType();  //获取当前阶段
                    }
                    for (int i = 1 ; i <= phase; i++) {
                        for (int groupId : groupIdList) {
                            CrossUnionWarRankListModel rankListModel = CrossUnionWarRankMgr.getCrossUnionWarRankListModel(activityInfo.getActivityId(), i, groupId);
                            if (rankListModel != null) {
                                syncList.add(rankListModel);
                            }
                        }
                    }
                }
                lastCheckTime = nowTime;
            } else {
                for (ActivityInfo activityInfo : activityInfoMap.values()) {
                    UnionQunyingTimeInfo timeInfo = CrossUnionWarActivityMgr.getCurrentTimeInfo(activityInfo.getActivityId()).getTimeInfo();
                    if (timeInfo == null) {
                        continue;
                    }
                    int phase = timeInfo.getType();  //获取当前阶段
                    List<Integer> groupIdList = CrossUnionWarMgr.getGroupListByCrossServerId(activityInfo.getActivityId(), CrossBaseServer.getInstance().getServerId());
                    for (int groupId : groupIdList) {
                        CrossUnionWarRankListModel rankListModel = CrossUnionWarRankMgr.getCrossUnionWarRankListModel(activityInfo.getActivityId(), phase, groupId);
                        if (null != rankListModel && rankListModel.isNeedSync()) {
                            syncList.add(rankListModel);
                        }
                    }
                }
            }
            if (syncList.size() > 0) {
                for (CrossUnionWarRankListModel rankListModel : syncList) {
                    CrossActivityProto.UnionWarRankMsgMsg.Builder rankMsg = CrossActivityProto.UnionWarRankMsgMsg.newBuilder();
                    rankMsg.setActivityId(rankListModel.getActivityId());
                    rankMsg.setPhase(rankListModel.getPhase());
                    rankMsg.setRankList(rankListModel.getRankListMsg());
                    rankMsg.setGroupId(rankListModel.getGroupId());
                    rankListModel.setNeedSync(false);
                    syncToGameServer(rankMsg, activityInfoMap.get(rankListModel.getActivityId()));
                }
            }
            isRun = false;
        }
    }

    private void syncToGameServer(CrossActivityProto.UnionWarRankMsgMsg.Builder rankMsg, ActivityInfo activityInfo) {
        if(null == activityInfo || null == rankMsg){
            return;
        }
        for (long serverId : activityInfo.getServerIdList()) {
            MessageHelper.sendPacket(serverId, 0, YanQuMessageUtils.buildMessage(Protocol.S_UNION_WAR_SYNC_RANK, rankMsg));
        }
    }
}
